package itheima.chap08;

public class ArrayTraversalDemo {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};

        printArray(arr);

        System.out.println(toString(arr));
    }

    // 需求1：定义方法.用whil循环遍历并打印int数值
    public static void printArray(int[] arr){
        int i = 0;
        while (i < arr.length){
            System.out.println(arr[i]);
            i++;
        }
    }
    // 需求2：定义方法.用while循环遍历int数组，并将数组元素拼接为[1,2,3]格式的字符串返回
    public static String toString(int[] arr){
        int i = 0;
        String result = "[";
        while (i < arr.length){
            result += arr[i];
            if (i != arr.length - 1){
                result += ",";
            }
            i++;
        }
        result += "]";
        return result;
    }
}
